<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Recos-RPC服务详情</title>
        <meta name="description" content="Redis-注册中心-服务详情页">
        <meta name="keywords" content="Redis-注册中心-服务详情页">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
<!--        <link rel="stylesheet" href="../css/sidebar.css">-->
        <link href="../plugins/quill/quill.snow.css" rel="stylesheet">
        <link href="../plugins/quill/quill.bubble.css" rel="stylesheet">
        <link href="../plugins/quill/quill.core.css" rel="stylesheet">
        <script src="../plugins/quill/quill.js"></script>

    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1 aria-label="center">服务详情页-{{appName}}</h1>
            </div>
            <br>
            <div class="app-container">
                <div class="box">
                    <el-tree
                             :data="treeNode"
                             :props="defaultProps"
                             :render-after-expand="false"
                             :expand-on-click-node="false"
                             @node-click="handleNodeClick"
                    >
                    </el-tree>
                    <el-dialog :visible="dialogVisible" :title="dialogTitle" @close="dialogVisible = false">
                        <el-button type="primary" @click="handleReconnect">重连</el-button>
                        <el-button type="danger" @click="handleDelete">断开</el-button>
                        <el-button @click="handleCancel0">取消</el-button>
                    </el-dialog>
                </div>
            </div>
        </div>
    </body>
    <script src="../js/vue.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../plugins/quill/vue-quill-editor.js"></script>
    <script src="../js/token.js"></script>
    <script>

        var vue = new Vue({
            el: '#app',
            data:{
                appName:'default',
                tarName:'default',
                dialogTitle:'连接操作',
                dialogVisible: false,
                redisServerPath: "/redisServer",
                recosServerPath: "/recos/server",
                treeNode: [{
                    label: '基本信息',
                    children:[
                        {
                            label: '是否存活',
                            children:[{
                                label:''
                            }]
                        },
                        {   label: '服务名',
                            children:[{
                                label:''
                            }]
                        },
                        {   label: 'url',
                            children:[{
                                label:''
                            }]
                        },
                        {   label: '权值',
                            children:[{
                                label:''
                            }]
                        },
                    ],
                }, {
                    label: '提供的服务',
                    children: []
                }, {
                    label: '引用(需要)的服务',
                    children: []
                },{
                    label: '连接情况',
                    children: []
                }
                ],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                },
            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                if (!getToken() || getToken()==''){
                    window.location.href="./login.html"

                }
                this.initPage();
            },
            methods: {
                initPage(){
                    function getQueryString(name) {
                        let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
                        let r = window.location.search.substr(1).match(reg);
                        if (r != null) {
                            return decodeURIComponent(r[2]);
                        };
                        return null;
                    }
                    var appName=getQueryString("appName")
                    var token=getQueryString("token")
                    // setToken(token)
                    this.appName=appName
                    axios.get(this.recosServerPath+"/getByappName/"+appName,getAxiosConfig()).then((res)=>{
                        if (res.data.success){
                            console.log(res.data)
                            this.treeNode[0].children[0].children[0].label=res.data.data.appBean.alive;
                            this.treeNode[0].children[1].children[0].label=res.data.data.appBean.appName;
                            this.treeNode[0].children[2].children[0].label=res.data.data.appBean.url;
                            this.treeNode[0].children[3].children[0].label=res.data.data.appBean.weight;

                            var rs=res.data.data.registedServices;
                            var rrs=[]
                            for (i = 0; i < rs.length; i++) {
                                cld={
                                    label:rs[i]
                                }
                                rrs.push(cld)
                            }
                            this.treeNode[1].children=rrs

                            var ns=res.data.data.needingsServices;
                            var nns=[]
                            for (i = 0; i < ns.length; i++) {
                                var cld={
                                    label:ns[i]
                                }
                                nns.push(cld)
                            }
                            this.treeNode[2].children=nns

                            var connects=[]
                            // var connectsData=['app-admin','app-harvest']
                            var connectsData=res.data.data.connectings
                            for (let i = 0; i < connectsData.length; i++) {
                                var con={
                                    label:connectsData[i],
                                    parent: this.treeNode[3]
                                }
                                connects.push(con)
                            }
                            this.treeNode[3].children=connects
                        }else if (res.data.code==-555){
                            window.location.href="./login.html";
                        }
                    })
                },
                handleNodeClick(data) {
                    console.log(data)
                    console.log(data.parent)
                    if (data.parent && data.parent === this.treeNode[3]) {
                        this.tarName=data.label;
                        this.dialogTitle=this.appName+"-连接-"+this.tarName
                        this.dialogVisible = true;
                    }
                },
                handleReconnect() {
                    axios.get(this.recosServerPath+"/reconnect/"+this.appName+"/"+this.tarName,getAxiosConfig()).then(
                        (res)=>{
                            alert(res.data.msg)
                        }
                    )
                    this.dialogVisible = false; // 关闭弹窗
                },
                handleDelete() {
                    // 处理删除逻辑
                    axios.delete(this.recosServerPath+"/disconnect/"+this.appName+"/"+this.tarName,getAxiosConfig()).then(
                        (res)=>{
                            alert(res.data.msg)
                        }
                    )
                    this.dialogVisible = false; // 关闭弹窗
                },
                handleCancel0(){
                    this.dialogVisible = false
                    this.tarName='default';
                }

            }
        });
        Vue.use(window.VueQuillEditor);
    </script>
</html>